package com.xfc.sde.component;


import com.xfc.sde.dto.MultiRouteDataSource;
import com.xfc.sde.utils.JDBCUtil;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DataSourceComponent{
    @Autowired
    private JDBCUtil jdbcUtil;
    @Primary//表示优先被注入
    @Bean(name = "multiDataSource")
    public MultiRouteDataSource exampleRouteDataSource() {
        MultiRouteDataSource multiDataSource = new MultiRouteDataSource();
        ResultSet resultSet = null;
        Statement statement = null;
        Connection connection = null;
        try {
            //采用jdbc访问主数据库
            connection = jdbcUtil.getConnection();
            statement = connection.createStatement();
            String sql = "select * from dynamic_data_source";
            resultSet = statement.executeQuery(sql);
            Map<Object, Object> targetDataSources = new HashMap<>();
            //遍历循环
            while (resultSet.next()) {
                //数据库url
                String url = resultSet.getString("url");
                //用户名
                String userName = resultSet.getString("user_name");
                //密码
                String password = resultSet.getString("password");
                //数据源名称
                String connection_name = resultSet.getString("connection_name");
                //驅動
                String driverClassName= resultSet.getString("driver_class_name");
                //创建Hikari数据库连接池
                HikariDataSource dataSource = new HikariDataSource();
                dataSource.setJdbcUrl(url);
                dataSource.setUsername(userName);
                dataSource.setPassword(password);
                dataSource.setDriverClassName(driverClassName);

                //Hikari数据池的配置
                dataSource.addDataSourceProperty("initialSize",8);
                dataSource.addDataSourceProperty("minIdle",5);
                dataSource.addDataSourceProperty("maxActive",20);
                dataSource.addDataSourceProperty("maxWait",60000);
                dataSource.addDataSourceProperty("timeBetweenEvictionRunsMillis",60000);
                dataSource.addDataSourceProperty("minEvictableIdleTimeMillis",300000);

                //把datasource放入map 多数据源每个key对应一个数据源
                targetDataSources.put(connection_name,dataSource);
                //数据库留有一条主数据源
                if(connection_name.equals("master")){
                    //把此主数据源设置为默认加载
                    multiDataSource.setDefaultTargetDataSource(dataSource);
                }
            }

            // 设置多数据源. key value的形式
            multiDataSource.setTargetDataSources(targetDataSources);
            return multiDataSource;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //释放资源
            jdbcUtil.releaseResc(resultSet, statement, connection);
        }
        return null;
    }



}

